/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClockPanel.java
 *
 * Created on 03.02.2010, 17:25:40
 */
package worldclock;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Calendar;

import javax.swing.JPanel;

import swinglayout.GroupLayout;
import swinglayout.LayoutStyle;

/**
 */
public class ClockPanel extends JPanel implements PropertyChangeListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private City city = City.Berlin;
	private String cityName = City.Berlin.getName();
	private long offset = City.Berlin.getOffset();

	/** Creates new form ClockPanel */
	public ClockPanel() {
		initComponents();
		cityLabel.setText(cityName);
		clock.addPropertyChangeListener(this);
		setPreferredSize(new java.awt.Dimension(358, 74));
	}

	public City getCity() {
		return this.city;
	}

	public void setCity(City city) {
		this.city = city;
		this.cityName = city.getName();
		this.offset = city.getOffset();

		long localOffset = Calendar.getInstance().get(Calendar.ZONE_OFFSET);
		long diff = localOffset + offset;
		int minDiff = (int) (diff % 3600000);
		int hourDiff = (int) (diff / 3600000);

		this.clock.setTimeZoneOffsetHour(hourDiff);
		this.clock.setTimeZoneOffsetMinute(minDiff);

		cityLabel.setText(this.cityName);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated
	// Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		cityLabel = new TextLabel();
		dayLabel = new TextLabel();
		sunMoon = new SunMoon();
		clock = new AnalogClock();

		setOpaque(false);
		setPreferredSize(new java.awt.Dimension(358, 100));
		setSize(new java.awt.Dimension(305, 100));

		cityLabel.setText("City");
		cityLabel.setFont(new java.awt.Font("Arial", 0, 36));
		cityLabel.setName("cityLabel"); // NOI18N

		dayLabel.setText("today");
		dayLabel.setFont(new java.awt.Font("Arial", 0, 18));
		dayLabel.setName("dayLabel"); // NOI18N

		sunMoon.setName("sunMoon"); // NOI18N

		GroupLayout sunMoonLayout = new GroupLayout(sunMoon);
		sunMoon.setLayout(sunMoonLayout);
		sunMoonLayout.setHorizontalGroup(sunMoonLayout.createParallelGroup(
				GroupLayout.LEADING).add(0, 24, Short.MAX_VALUE));
		sunMoonLayout.setVerticalGroup(sunMoonLayout.createParallelGroup(
				GroupLayout.LEADING).add(0, 24, Short.MAX_VALUE));

		clock.setName("clock"); // NOI18N
		clock.setPreferredSize(new java.awt.Dimension(74, 74));

		GroupLayout clockLayout = new GroupLayout(clock);
		clock.setLayout(clockLayout);
		clockLayout.setHorizontalGroup(clockLayout.createParallelGroup(
				GroupLayout.LEADING).add(0, 74, Short.MAX_VALUE));
		clockLayout.setVerticalGroup(clockLayout.createParallelGroup(
				GroupLayout.LEADING).add(0, 74, Short.MAX_VALUE));

		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.add(clock, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(LayoutStyle.RELATED)
										.add(
												layout
														.createParallelGroup(
																GroupLayout.LEADING)
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				sunMoon,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				LayoutStyle.RELATED)
																		.add(
																				dayLabel,
																				GroupLayout.PREFERRED_SIZE,
																				148,
																				GroupLayout.PREFERRED_SIZE)
																		.addContainerGap())
														.add(
																cityLabel,
																GroupLayout.DEFAULT_SIZE,
																276,
																Short.MAX_VALUE))));
		layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.LEADING)
				.add(
						layout.createSequentialGroup().add(cityLabel,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE).addPreferredGap(
								LayoutStyle.RELATED, GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE).add(
								layout
										.createParallelGroup(
												GroupLayout.TRAILING).add(
												sunMoon,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.add(dayLabel,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)))
				.add(clock, GroupLayout.PREFERRED_SIZE,
						GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE));
	}// </editor-fold>//GEN-END:initComponents

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private TextLabel cityLabel;
	private AnalogClock clock;
	public TextLabel dayLabel;
	private SunMoon sunMoon;

	// End of variables declaration//GEN-END:variables

	@Override
	public void propertyChange(PropertyChangeEvent event) {
		if (event.getSource().equals(clock)) {
			if (event.getPropertyName().equals("dayOffset")) {
				switch ((Integer) event.getNewValue()) {
				case -1:
					dayLabel.setText("yesterday");
					break;
				case 0:
					dayLabel.setText("today");
					break;
				case 1:
					dayLabel.setText("tomorrow");
					break;
				default:
					dayLabel.setText("today");
					break;
				}
			}

			if (event.getPropertyName().equals("timeOfDay")) {
				switch ((Integer) event.getNewValue()) {
				case -2: // Moon
					sunMoon.setTimeOfDay(-2);
					break;
				case -1: // Sunrise
					sunMoon.setTimeOfDay(-1);
					break;
				case 0: // Sun
					sunMoon.setTimeOfDay(0);
					break;
				case 1: // Sunset
					sunMoon.setTimeOfDay(1);
					break;
				default:
					sunMoon.setTimeOfDay(0);
					break;
				}
			}
		}
	}
}
